<template>
  <el-container class="home-box">
    <el-header>
      <div>
        <img src="./assets/dlam.jpg" alt="" />
        <h3>电商后台管理系统</h3>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px': '200px' " class="aside">
          <div class="op-cl" @click="zhedie">| | |</div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="icons[item.id]" ></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index=" '/'+ subItem.path + ''" v-for="subItem in item.children" :key="subItem.id"
            @click="navName('/'+ subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
          <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  components: {},
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('active')
  },
  data() {
    return {
      isCollapse:false,
      menuList: [],
      icons:{
          '125':'iconfont icon-haoyou',
          '103':'iconfont icon-quanxianfenji',
          '101':'iconfont icon-shangpin',
          '102':'iconfont icon-dingdandingdanmingxishouzhimingxi',
          '145':'iconfont icon-bg-data'
      },
      activePath:'',
    };
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
    //   console.log(res);
      if (res.meta.status !== 200) return this.$message.error("获取数据失败");
      this.menuList = res.data;
    },
    zhedie(){
        this.isCollapse = !this.isCollapse;
    },
    navName(active){
        window.sessionStorage.setItem('active', active)
        this.activePath = active
    },
  },
};
</script>

<style lang="scss" scoped>
.home-box {
  height: 100%;
  .el-header {
    width: 100%;
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      img {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin-right: 30px;
      }
      h3 {
        color: white;
      }
    }
    .el-button {
      height: 40px;
    }
  }
  .el-aside {
    background-color: #333744;
    overflow-x: hidden;
    .op-cl{
        background-color: #4A5064;
        color: #fff;
        text-align: center;
        padding: 3px 0;
        cursor: pointer;
        user-select: none;
    }
    .el-menu {
      width: 100%;
      background-color: #333744 !important;
      border-right: 0;
              user-select: none;
      .iconfont{
          margin-right: 5px;
      }
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>